package sushil.luc.gui;



import sushil.luc.msc.UgroKitActivity;
import sushil.luc.truckersmartrfid.R;
import com.ugrokit.api.Ugi;
import com.ugrokit.api.UgiInventoryDelegate;
import com.ugrokit.api.UgiTag;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends UgroKitActivity implements
Ugi.ConnectionStateListener{
	

	public static Context appContext;
	private static String log ="MainActivity";
	private ActionBar actionbar;
	public static  final String ScanItemTabName ="Scan Items";
	public static final String MapTabName ="Map";
    public static String HOST_URL = "http://sushilshilpakar.com.np/smartrfid";
	
	private MyTabsListener<ScanItemFragment> TabListenerScanItem;
	private MyTabsListener<TruckerMapFragment> TabListenerMap;
	
/**
 * init the Tabs 
 */
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);

     // setup action bar for the tabs
	 actionbar = getActionBar();
	 actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
	 actionbar.setSubtitle(currentStatus);
	 actionbar.removeAllTabs();
	 
     if (actionbar.getTabCount() == 0)
     {
    	 actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
         
         ActionBar.Tab ScanItemTab = actionbar.newTab().setText(ScanItemTabName);
         ActionBar.Tab MapTab = actionbar.newTab().setText(MapTabName);       
         
         // create Tab Listener
         TabListenerScanItem = new MyTabsListener<ScanItemFragment>(this, ScanItemTabName, ScanItemFragment.class);
         ScanItemTab.setTabListener(TabListenerScanItem);
         
         TabListenerMap = new MyTabsListener<TruckerMapFragment>(this, MapTabName, TruckerMapFragment.class);
         MapTab.setTabListener(TabListenerMap);
         
         // add the tabs to the actionbar
         actionbar.addTab(ScanItemTab);
         actionbar.addTab(MapTab);

     }
     
     // check if there is any saved value, so we can jump pack to the last selected tab. If not take the first one
     if (savedInstanceState != null)
     {
    	 actionbar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
     }
	}

	@Override
 	protected void onSaveInstanceState(Bundle outState)
 	{
		super.onSaveInstanceState(outState);
		// Save the last opened tab
		outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
 	}
	
	@Override
	/**
	 * If the connection from the ugrokit changes, this methode gives feedback to the user
	 */
	public void connectionStateChanged(Ugi.ConnectionStates connectionState) {
		super.connectionStateChanged(connectionState);
		// update the StatusBar
		super.calculateStatus();
		notifiySatusUpdate();
	}
	
	/**
	 * Update the status bar
	 */
	public void notifiySatusUpdate()
	{
		if (actionbar!=null)
			actionbar.setSubtitle(currentStatus);
	}
	
	public void onResume()
	{
		super.onResume();
		//Stop the Inventory. Consumes power and is not needed here
		super.StopInventory();
	}
	


}
